package me.algo.st;

import java.util.TreeSet;

/**
 * @author guozheng
 * @date 2024/5/6
 */
public class St1 {

    public static void main(String[] args) {
        SmallestInfiniteSet set = new SmallestInfiniteSet();
        set.addBack(2);
        System.out.println(set.popSmallest());
        System.out.println(set.popSmallest());
        System.out.println(set.popSmallest());
        set.addBack(1);
        System.out.println(set.popSmallest());
        System.out.println(set.popSmallest());
        System.out.println(set.popSmallest());
    }

}


class SmallestInfiniteSet {


    private TreeSet<Integer> set;

    private int smallest;



    public SmallestInfiniteSet() {
        set = new TreeSet<>();
        smallest = 1;
    }

    public int popSmallest() {
        if (set.isEmpty()) {
            int rtn = smallest;
            smallest++;
            return rtn;
        }
        return set.removeFirst();
    }

    public void addBack(int num) {
        if (num < smallest) {
            set.add(num);
        }
    }
}
